//
//  QWCommonMethod.m
//  qwm
//
//  Created by kevin on 2023/3/26.
//

#import "QWCommonMethod.h"
#import "QWMainTabBarController.h"
#import "QWCLWrapper.h"
#import "JieqiDataModel.h"

@implementation QWCommonMethod
static QWCommonMethod *_sharedInstance = nil;
static dispatch_once_t once_token = 0;
+(instancetype)Instance {
    dispatch_once(&once_token, ^{
        if (_sharedInstance == nil) {
            _sharedInstance = [[super allocWithZone:NULL] init] ;
        }
    });
    return _sharedInstance;
}


#pragma mark -showAlert提示
-(void)ShowAlert:(NSString*)title Msg:(NSString*)msg
{
    UIAlertView* alert=[[UIAlertView alloc] initWithTitle:title
                                                  message:msg
                                                 delegate:self
                                        cancelButtonTitle:@"确定"
                                        otherButtonTitles:nil, nil];
    [alert show];
}

#pragma mark - 当前版本号
- (NSString *)currentVersion{
    NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    return version;
}

#pragma mark - 获取当前屏幕显示的viewcontroller
- (UIViewController *)getCurrentVC {
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows) {
            if (tmpWin.windowLevel == UIWindowLevelNormal) {
                window = tmpWin;
                break;
            }
        }
    }
    
    if ([window subviews].count ==0 )return nil;
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]]) {
        result = nextResponder;
    } else {
        result = window.rootViewController;
    }
    
    /*
     *  在此判断返回的视图是不是你的根视图--我的根视图是tabbar
     */
    if ([result isKindOfClass:[QWMainTabBarController class]]) {
        QWMainTabBarController *mainTabBarVC = (QWMainTabBarController *)result;
        result = [mainTabBarVC selectedViewController];
        result = [result.childViewControllers lastObject];
    }
    
    return result;
}

#pragma mark- 时间转换格式
-(NSString *)changeDateToString:(NSDate *)date withFormatter:(NSString *)formatterStr
{
    NSString *timeStr =@"";
    if (date) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = formatterStr;
        timeStr = [dateFormatter stringFromDate:date];
    }
    return timeStr;
}

#pragma mark- 计算当前时间在哪个节气间
-(NSString *)calculateThisYearSolarTerms
{
    NSDate *nowDate=[NSDate date];
    NSString *year =[self changeDateToString:nowDate withFormatter:@"YYYY"];
    NSMutableArray *arr = [CLWrapper calculateAllJieqiByYear:[year intValue] fromJieqiId:0]; //21 是立春，详见 calendartool.h 中的定义
    
    JieqiDataModel* currentjdModel;
    for (int i=0; i<arr.count; i++) {
        JieqiDataModel* jdModel=arr[i];
        NSDate *laterDate = [jdModel.jieqiDate laterDate:nowDate];
        //当前时间处于两个节气之间
        if ([laterDate isEqualToDate:jdModel.jieqiDate]) {
            if (i==0) {
                currentjdModel=arr[arr.count-1];
                currentjdModel.numOfAllJieqi=arr.count;
            }else
            {
                currentjdModel=arr[i-1];
                currentjdModel.numOfAllJieqi=i;
            }
            return currentjdModel.jieqiName;
        }
    }
    return nil;
}

#pragma mark- 计算当前时间在哪个时辰-修改时辰状态
-(NSString *)calculateOneOfTheTwelveHourPeriods
{
    NSInteger hourNum =[[self changeDateToString:[NSDate date] withFormatter:@"H"] integerValue];
    
    NSInteger currentHourNum;
    if (hourNum%2==0) {
        currentHourNum=hourNum/2;
    }else
    {
        currentHourNum=hourNum/2+1;
    }
    if (currentHourNum>=12) {
        currentHourNum=0;
    }
    
    NSArray *arr =[[NSArray alloc]initWithObjects:
                            @"子时",@"丑时",@"寅时",@"卯时",
                            @"辰时",@"巳时",@"午时",@"未时",
                            @"申时",@"酉时",@"戌时",@"亥时",nil];
    NSString *showStr;
    if (currentHourNum<arr.count) {
        showStr=arr[currentHourNum];
    }
    return showStr;
}


@end
